package de.tum.in.eist.poll.client.gui;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.Widget;

public class WidgetContainer extends FlexTable {

	/**
	 * A {@link Button} to handle an event like hide a notification or remove a
	 * widget.
	 */
	private PushButton hideButton;

	/**
	 * A {@link Button} to minimize or maximize a widget.
	 */
	private PushButton minimizeButton;

	/**
	 * Create an container to hold a widget which has whether a minimize and/or
	 * a hide button or not.<br>
	 * The CSS-Styles are in the following way defined: <li>
	 * <code>containerLeft</code> the left side of the container which holds the
	 * minimize button</li> <li><code>containerCenter</code> the center of the
	 * container which holds the widget</li> <li><code>containerRight</code> the
	 * right side of the container which holds the hide button</li>
	 * 
	 * @param widget
	 *            the widget to be hold
	 * @param minimizeButton
	 *            <code>true</code> it has a minimize button, if
	 *            <code>false</code> not
	 * @param hideButton
	 *            <code>true</code> it has a hide button, if <code>false</code>
	 *            not
	 */
	public WidgetContainer(Widget widget, boolean minimizeButton,
			boolean hideButton) {
		//super(1, 3);
		
		if (minimizeButton) {
			addMinimizeButton();
		}
		if (hideButton) {
			addHideButton();
		}

		setWidget(0, 1, widget);
		
		getFlexCellFormatter().addStyleName(0, 0, "containerLeft");
		getFlexCellFormatter().addStyleName(0, 1, "containerCenter");
		getFlexCellFormatter().addStyleName(0, 2, "containerRight");
		setStyleName("containerForm");
	}

	/**
	 * Create an container to hold a widget which has whether a minimize and/or
	 * a hide button or not.<br>
	 * The CSS-Styles are in the following way defined: <li>
	 * <code>containerLeft</code> the left side of the container which holds the
	 * minimize button</li> <li><code>containerCenter</code> the center of the
	 * container which holds the widget</li> <li><code>containerRight</code> the
	 * right side of the container which holds the hide button</li>
	 * 
	 * @param minimizeButton
	 *            <code>true</code> it has a minimize button, if
	 *            <code>false</code> not
	 * @param hideButton
	 *            <code>true</code> it has a hide button, if <code>false</code>
	 *            not
	 */
	public WidgetContainer(boolean minimizeButton, boolean hideButton) {
		this(null, minimizeButton, hideButton);
	}

	/**
	 * Create an empty container to hold a widget
	 * 
	 * @see #WidgetContainer(Widget, boolean, boolean)
	 */
	public WidgetContainer() {
		this(null, false, false);
	}

	/**
	 * Add the widget to the center of this container
	 * 
	 * @param widget
	 *            the widget to be added
	 */
	public void addWidget(Widget widget) {
		setWidget(0, 1, widget);
	}

	/**
	 * Add the widget to the center of this container
	 * 
	 * @param widget
	 *            the widget to be added
	 */
	@Override
	public void add(Widget widget) {
		addWidget(widget);
	}

	/**
	 * Create a {@link #hideButton}, add a {@link ClickHandler} to it and add it
	 * to the container.
	 */
	public void addHideButton(ClickHandler handler) {
		if (hideButton == null)
			hideButton = new PushButton(new Image("/images/plusButton.png"));
		hideButton.addStyleName("containerHideButton");
		addHideButtonClickHandler(handler);
		setWidget(0, 2, hideButton);
	}

	/**
	 * Create a {@link #hideButton} and add it to the container.
	 */
	public void addHideButton() {
		addHideButton(null);
	}

	/**
	 * Create a {@link #minimizeButton}, add a {@link ClickHandler} to it and
	 * add it to the container.
	 * 
	 * @param handler
	 *            the {@link ClickHandler} to add
	 */
	public void addMinimizeButton(ClickHandler handler) {
		minimizeButton = new PushButton(new Image("/images/DropDownButton.png"));
		minimizeButton.addStyleName("containerMinimizeButton");
		addMinimizeButtonClickHandler(handler);
		setWidget(0, 0, minimizeButton);
	}

	/**
	 * Create a {@link #minimizeButton} and add it to the container.
	 */
	public void addMinimizeButton() {
		addMinimizeButton(null);
	}

	/**
	 * Add a {@link ClickHandler} to the {@link #hideButton} {@link Button} if
	 * it's not null.
	 * 
	 * @param handler
	 *            the {@link ClickHandler} to add
	 */
	public void addHideButtonClickHandler(ClickHandler handler) {
		if (hideButton != null && handler != null) {
			hideButton.addClickHandler(handler);
		}
	}

	/**
	 * Add a {@link ClickHandler} to the {@link #minimizeButton} {@link Button}
	 * if it's not null.
	 * 
	 * @param handler
	 *            the {@link ClickHandler} to add
	 */
	public void addMinimizeButtonClickHandler(ClickHandler handler) {
		if (minimizeButton != null && handler != null) {
			minimizeButton.addClickHandler(handler);
		}
	}
	
	/**
	 * Remove the {@link #hideButton} from this container
	 */
	public void removeHideButton() {
		if (hideButton != null)
			remove(hideButton);
	}

	/**
	 * @return the {@link #hideButton} of this container
	 */
	public PushButton getHideButton() {
		return hideButton;
	}

	/**
	 * @return the {@link #minimizeButton} of this container
	 */
	public PushButton getMinimizeButton() {
		return minimizeButton;
	}
}
